package com.ruoyi.goods.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.goods.domain.Preempt;
import com.ruoyi.goods.domain.Products;
import com.ruoyi.goods.mapper.PreemptMapper;
import com.ruoyi.goods.mapper.ProductsMapper;
import com.ruoyi.goods.service.IPreemptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 商品抢兑Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class PreemptServiceImpl implements IPreemptService
{
    @Autowired
    private PreemptMapper preemptMapper;
   @Autowired
   private ProductsMapper productsMapper;
    /**
     * 查询商品抢兑
     * 
     * @param id 商品抢兑主键
     * @return 商品抢兑
     */
    @Override
    public Preempt selectPreemptById(Long id)
    {
        return preemptMapper.selectPreemptById(id);
    }

    @Override
    public List<Preempt> selectPreemptTime(Date time) {
        return preemptMapper.selectPreemptTime(time);
    }

    /**
     * 查询商品抢兑列表
     * 
     * @param preempt 商品抢兑
     * @return 商品抢兑
     */
    @Override
    public List<Preempt> selectPreemptList(Preempt preempt)
    {



        return preemptMapper.selectPreemptList(preempt);
    }

    /**
     * 新增商品抢兑
     * 
     * @param preempt 商品抢兑
     * @return 结果
     */
    @Override
    public int insertPreempt(Preempt preempt)
    {
        // 创建 Products 对象并设置条件（确保只更新特定产品）
        Long productId = preempt.getProductId(); // 假设 Preempt 有 productId 属性
            Products products = new Products();
            // 更新产品的类型ID
            products.setId(productId);
            products.setState(1);
            productsMapper.updateProducts(products); // 更新产品


        // 插入 Preempt 记录
        return preemptMapper.insertPreempt(preempt);
    }

    /**
     * 修改商品抢兑
     * 
     * @param preempt 商品抢兑
     * @return 结果
     */
    @Override
    public int updatePreempt(Preempt preempt)
    {
        return preemptMapper.updatePreempt(preempt);
    }

    /**
     * 批量删除商品抢兑
     * 
     * @param ids 需要删除的商品抢兑主键
     * @return 结果
     */
    @Override
    public int deletePreemptByIds(Long[] ids)
    {

        return preemptMapper.deletePreemptByIds(ids);
    }

    /**
     * 删除商品抢兑信息
     * 
     * @param id 商品抢兑主键
     * @return 结果
     */
    @Override
    public int deletePreemptById(Long id)
    {
        Products products = new Products();
        products.setId(id);
        products.setState(0);
        productsMapper.updateProducts(products);
        return preemptMapper.deletePreemptById(id);
    }
}
